/*
Copyright (C) 2022-2024 Inspur Corp.

SPDX-License-Identifier: Apache-2.0
*/
package rdb

import (
	"github.com/go-redis/redis"
	"icep/common/logger"
	config2 "icep/config"
)

var log = logger.GetLogger("common.rdb")

type RedisConfig struct {
	Addr string `json:"addr"`
	Password string `json:"password"`
	Database int `json:"database"`
}

func loadConfig() *RedisConfig {
	rc := &RedisConfig{}
	rc.Addr = config2.Get("icep.redis.addr", "localhost:6379")
	rc.Password = config2.Get("icep.redis.password", "")
	rc.Database = config2.GetInt("icep.redis.db", 0)
	
	return rc
}

func GetRedisClient() *redis.Client {
	redisConfig := loadConfig()
	client := redis.NewClient(&redis.Options{
		Addr:     redisConfig.Addr,
		Password: redisConfig.Password,
		DB:       redisConfig.Database,
	})

	_, err := client.Ping().Result()
	if err != nil {
		log.Errorf("connect redis server error: %s", err)
		return nil
	}

	return client
}

